package com.atguigu.srb.core.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;

import javax.annotation.PostConstruct;
import java.util.Base64;

@Configuration
@Slf4j
public class RabbitMqConfig {

    @Autowired
    RabbitTemplate rabbitTemplate;
    @PostConstruct
    public void init(){
        //消息到达交换机的回调
        rabbitTemplate.setConfirmCallback(( CorrelationData correlationData,
                                            boolean ack, String cause)->{
            if(!ack){
                log.error("消息未到达交换机: cause - {}",cause);
            }
        });
        //消息未到达队列时回调
        rabbitTemplate.setReturnCallback((Message message, int replyCode,
                                          String replyText, String exchange, String routingKey)->{
            log.error("消息未到达队列：replyCode - {},replyText - {},exchange - {},routingKey - {},message - {}",replyCode,
                    replyText,exchange,routingKey , Base64.getEncoder().encode(message.getBody()));
        });
    }
}
